﻿using UnityEngine;
using UnityEngine.UI;

using System.Collections;
using UnityEditor;
using UnityEditorInternal;
using System;

public class UGUIMenu  : Editor {

    [MenuItem("Edit/UI/AnchorsToCorners %[")]
    static void AnchorsToCorners()
    {
        foreach (RectTransform transform in Selection.transforms)
        {
            RectTransform t = transform as RectTransform;
            RectTransform pt = transform.parent as RectTransform;

            if (t && pt)
            {
                Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
                                                t.anchorMin.y + t.offsetMin.y / pt.rect.height);
                Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
                                                    t.anchorMax.y + t.offsetMax.y / pt.rect.height);
                t.anchorMin = newAnchorsMin;
                t.anchorMax = newAnchorsMax;
                t.offsetMin = t.offsetMax = new Vector2(0, 0);
            }
        }
    }

    [MenuItem("Edit/UI/CornersToAnchors %]")]
    static void CornersToAnchors()
    {
        foreach (RectTransform transform in Selection.transforms)
        {
            RectTransform t = transform as RectTransform;
            if (t)
                t.offsetMin = t.offsetMax = new Vector2(0, 0);
        }
    }

    [MenuItem("Edit/UI/ToggleImageControl %t")]
    static void ToggleImageControl()
    {
        foreach (GameObject gameObject in Selection.gameObjects)
        {
            RawImage rawImage = gameObject.GetComponent<RawImage>();
            if(rawImage)
            {
                UnityEngine.Object.DestroyImmediate(rawImage);
                //GameObject.Destroy(rawImage);
                gameObject.AddComponent<Image>();
            }
            else
            {
                Image image = gameObject.GetComponent<Image>();
                if(image)
                {
                    UnityEngine.Object.DestroyImmediate(image);
                    gameObject.AddComponent<RawImage>();
                }
            }
        }
    }

    [MenuItem("Edit/UI/Replace/Button &%b")]
    static void ReplaceButton()
    {
        foreach (GameObject gameObject in Selection.gameObjects)
           gameObject.Replace<Button, SubjectiveButton>(); 
    }

    [MenuItem("Edit/UI/Replace/Image &%i")]
    static void ReplaceImage()
    {
        foreach (GameObject gameObject in Selection.gameObjects)
            gameObject.Replace<Image, SubjectiveImage>();
    }

    [MenuItem("Edit/UI/Replace/RawImage &%r")]
    static void ReplaceRawImage()
    {
        foreach (GameObject gameObject in Selection.gameObjects)
            gameObject.Replace<RawImage, SubjectiveRawImage>();
    }

    [MenuItem("Edit/UI/Replace/Text &%t")]
    static void ReplaceText()
    {
        foreach (GameObject gameObject in Selection.gameObjects)
            gameObject.Replace<Text, SubjectiveText>();
    }
}

public static class CopyExtension
{
    public static T GetRemovedCatchedComponent<T>(this GameObject gameObject) where T : Component
    {
        GameObject tempObject = new GameObject("TempCatchComp");
        T component = gameObject.GetComponent<T>();
        if (component)
        {
            ComponentUtility.CopyComponent(component);
            ComponentUtility.PasteComponentAsNew(tempObject);
            GameObject.DestroyImmediate(component);
            component = tempObject.GetComponent<T>();
        }
        return component;
    }

    public static void Replace<S,D>(this GameObject gameObject) where S : Component where D : Component
    {
        S source = gameObject.GetRemovedCatchedComponent<S>();
        if (source)
        {
            D destination= gameObject.AddComponent<D>();
            //destination.CopyFrom(source);
            CopyComponent(destination as D, source as S);
        }
        GameObject tempObject = GameObject.Find("TempCatchComp");
        if (tempObject) GameObject.DestroyImmediate(tempObject);
    }

    private static void CopyComponent<D, S>(D d, S s)  where D : Component where S : Component
    {
        if (s is Button)
            CopyButton(d as SubjectiveButton, s as Button);
        if (s is Image)
            CopyImage(d as SubjectiveImage, s as Image);
        if (s is RawImage)
            CopyRawImage(d as SubjectiveRawImage, s as RawImage);
        if (s is Text)
            CopyText(d as SubjectiveText, s as Text);
    }
    private static void CopyButton(SubjectiveButton destination, Button source)
    {
        destination.interactable = source.interactable;
        destination.transition = source.transition;
        destination.colors = source.colors;
    }

    public static void CopyText(SubjectiveText destination, Text source)
    {
        destination.text = source.text;
        destination.alignment = source.alignment;
        destination.font = source.font;
        destination.fontSize = source.fontSize;
        destination.fontStyle = source.fontStyle;
        destination.lineSpacing = source.lineSpacing;
        destination.supportRichText = source.supportRichText;
        destination.horizontalOverflow = source.horizontalOverflow;
        destination.verticalOverflow = source.verticalOverflow;
        destination.resizeTextForBestFit = source.resizeTextForBestFit;

        destination.color = source.color;
        destination.material = source.material;
    }
    public static void CopyImage(SubjectiveImage destination, Image source)
    {
        destination.sprite = source.sprite;
        destination.color = source.color;
        destination.type = source.type;
        destination.fillCenter = source.fillCenter;
    }
    public static void CopyRawImage(SubjectiveRawImage destination, RawImage source)
    {
        destination.texture = source.texture;
        destination.color = source.color;
        destination.material = source.material;
        destination.uvRect = source.uvRect;
    }
}